<?php
set_time_limit(0); 
error_reporting(0);
/*
Uploadify
Copyright (c) 2012 Reactive Apps, Ronnie Garcia
Released under the MIT License <http://www.opensource.org/licenses/mit-license.php> 
*/

// Define a destination
$targetFolder = '/Uploads'; // Relative to the root

//$verifyToken = md5('unique_salt' . $_POST['timestamp']);

if (!empty($_FILES)) {
    
    if($_GET['s'] && ($_GET['s']*1024)<$_FILES['Filedata']['size']){
        echo '3';exit;
    }
    $tempFile = $_FILES['Filedata']['tmp_name'];
    $targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder;
    $fileParts = pathinfo($_FILES['Filedata']['name']);
    $filename = time().mt_rand(10,99).'.'.end(explode('.', $_FILES['Filedata']['name'])); //在这里修改生出随机图片名
    $fileTypes = array('jpg','jpeg','gif','png','JPG','PNG'); // File extensions
    if (in_array($fileParts['extension'],$fileTypes)) {
        //上传图片到云端 start
        require_once("../qiniu/io.php");
        require_once("../qiniu/rs.php");

        $bucket = "qcttupload";//空间名
        //截取原始文件后缀名
        $key1 =$filename;
        $accessKey = 'BiX2Syn9tBIK3TygpYtit9BwitVa0RKQ68V9ca9y';//这里填写ak
        $secretKey = 'ydb11xMMZaf4UVATYp-ZAsxwixGvwBafTti6t9rx';// 这里填写SK

        Qiniu_SetKeys($accessKey, $secretKey);
        $putPolicy = new Qiniu_RS_PutPolicy($bucket);
        $upToken = $putPolicy->Token(null);
        $putExtra = new Qiniu_PutExtra();
        $putExtra->Crc32 = 1;
        //$tempFile uploadify上传的临时文件路径
        list($ret, $err) = Qiniu_PutFile($upToken,$key1,$tempFile);
        if(empty($err)){
           //上传图片到云端 end
                //返回文件名给前台
            require "../../../app/Libs/Constant.php";

                echo \App\Libs\Constant::IMG_DOMAIN.$key1; //前台使用回调函数的data参数接收
        }
        
    } else {
        echo '2';
    }
}
?>